package com.oig.sys.msg.file.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.oig.common.constants.CommonConstants;
import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.ServiceException;
import com.oig.sys.file.api.FileInfoService;
import com.oig.sys.file.dto.BusFileInfoDto;
import com.oig.sys.file.dto.FileInfoDto;
import com.oig.sys.file.vo.FileInfoVo;
import com.oig.sys.msg.dto.NoticeMsgFileDto;
import com.oig.sys.msg.file.IDepFileService;
import com.oig.sys.msg.vo.NoticeMsgFileVo;
import com.oig.sys.msg.vo.NoticeMsgVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class DepFileServiceImpl implements IDepFileService {

    private final FileInfoService fileInfoService;


    @Override
    public List<NoticeMsgFileVo> addOrUpdateAllFiles(NoticeMsgVo noticeMsg, List<NoticeMsgFileDto> noticeMsgFileDtoList) {
        if (CollectionUtil.isEmpty(noticeMsgFileDtoList)) return null;
        BusFileInfoDto busFileInfoDto = new BusFileInfoDto();
        busFileInfoDto.setBusinessType(CommonConstants.DEF_MIX_STR);
        busFileInfoDto.setRelationType(CommonEnum.RelationType.OIGX_MSG.getCode());
        busFileInfoDto.setRelationId(String.valueOf(noticeMsg.getId()));
        List<FileInfoDto> fileInfoDtoList = new ArrayList<>();
        for (NoticeMsgFileDto msgFileDto : noticeMsgFileDtoList){
            FileInfoDto fileInfoDto = new FileInfoDto();
            BeanUtils.copyProperties(msgFileDto, fileInfoDto);
            fileInfoDto.setBusinessType(CommonConstants.DEF_MIX_STR);
            fileInfoDto.setRelationType(CommonEnum.RelationType.OIGX_MSG.getCode());
            fileInfoDto.setRelationId(String.valueOf(noticeMsg.getId()));
            fileInfoDto.setTenantId(noticeMsg.getTenantId());
            fileInfoDto.setTenantName(noticeMsg.getTenantName());
            fileInfoDtoList.add(fileInfoDto);
        }
        log.info("request add file:{}",fileInfoDtoList);
        busFileInfoDto.setList(fileInfoDtoList);
        List<FileInfoVo> fileInfoVoList=fileInfoService.updateRelationAllFileInfo(busFileInfoDto); //可能会产生脏数据
        log.info("add file response:{}", fileInfoVoList);
        if (fileInfoVoList==null){      //如果配置了降级，返回null,需要一致就抛异常
            throw new ServiceException("附件添加失败") ;
        }
        List<NoticeMsgFileVo> fileVoList = new ArrayList<>();
        fileInfoVoList.forEach((item)->{
            NoticeMsgFileVo msgFileVo = new NoticeMsgFileVo();
            BeanUtils.copyProperties(item,msgFileVo);
            fileVoList.add(msgFileVo);
        });
        return fileVoList;
    }

    @Override
    public void delAllNoticeFiles(Integer noticeMsgId) {
        BusFileInfoDto busFileInfoDto = new BusFileInfoDto();
        busFileInfoDto.setBusinessType(CommonConstants.DEF_MIX_STR);
        busFileInfoDto.setRelationId(String.valueOf(noticeMsgId));
        busFileInfoDto.setRelationType(CommonEnum.RelationType.OIGX_MSG.getCode());
        fileInfoService.delRelationAllFileInfo(busFileInfoDto) ;
    }


}
